//
//  MyViewController.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/10.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SVProgressHUD
let myOrdersTableViewReuseIdentifier = "myOrdersTableViewReuseIdentifier"
class MyViewController: AbbBaseViewController {
    
    var myInfoViewWightAndHeight:(CGFloat,CGFloat) = (UIScreen.mainScreen().bounds.size.width,UIScreen.mainScreen().bounds.size.height * 0.3)
    
    var user:User? {
        didSet {
            myInfoView.user = user
        }
    }
    
    override var userTransparentNavigationBar: Bool {
        return true
    }
    
    var orders:[Order]? {
        didSet {
            myOrdersTableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        setupUI()
        loadOrderList()
        
        myInfoView.clickIconCallBack = { (image) in
            let vc = ShowMyIconViewController()
            vc.image = image
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func loadOrderList() {
        if let id = User.loadUser()?.id {
            Order.loadOrderList(id, finished: { (models, error) in
                if error != nil {
                   SVProgressHUD.showErrorWithStatus(error)
                    return
                }
                if models != nil {
                    self.orders = models
                    return
                }
                
            })
        }
    }
    
    private func setupUI() {
        // 设置顶部个人信息view的frame
        myInfoView.frame = CGRect(x: 0, y: 0, width: myInfoViewWightAndHeight.0, height: myInfoViewWightAndHeight.1)
        // 添加一个头部视图和个人信息相同大小
        myOrdersTableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0,width: myInfoViewWightAndHeight.0, height: myInfoViewWightAndHeight.1))
        // 将顶部个人信息view加到tableview
        myOrdersTableView.addSubview(myInfoView)
        // 添加tableview到父视图
        view.addSubview(myOrdersTableView)
        // 布局
        myOrdersTableView.snp_makeConstraints { (make) in
            make.bottom.left.right.top.equalTo(self.view)
        }
    }

    private lazy var myInfoView:MyInfoView = MyInfoView()
    
    private lazy var myOrdersTableView:UITableView = {
        let tb = UITableView(frame: CGRectZero, style: .Plain)
        tb.delegate = self
        tb.dataSource = self
        tb.registerClass(MyOrdersTableViewCell.self, forCellReuseIdentifier: myOrdersTableViewReuseIdentifier)
        return tb
    }()

}

extension MyViewController:UITableViewDataSource,UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return orders?.count ?? 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(myOrdersTableViewReuseIdentifier, forIndexPath: indexPath) as! MyOrdersTableViewCell
        cell.order = orders![indexPath.row]
        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 128
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return orders?.count > 0 ? 30 : 0
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 拿到位移Y
        let offsetY = scrollView.contentOffset.y
        if offsetY < 0 {
            // 计算需要变化的高度
            let totalOffsetHeifht = self.myInfoViewWightAndHeight.1 - offsetY
            // 重设frame
            myInfoView.frame = CGRect(x: 0, y: offsetY, width: self.myInfoViewWightAndHeight.0, height: totalOffsetHeifht)
        }
    }
    
    
}